package org.absolutezero.client.view;

import javax.inject.Inject;

import org.absolutezero.client.events.LoginEvent;
import org.absolutezero.client.events.RegisterEvent;
import org.absolutezero.client.presenter.RecipesPresenter;
import org.absolutezero.client.presenter.ShelvesPresenter;
import org.absolutezero.client.style.HeadingLabel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class PageContentView extends Composite{

	private final Label lblName;
	private final Label lblPassword;
	private final Label lblFullName;
	private final TextBox tbFullName;
	private final TextBox tbName;
	private final PasswordTextBox tbPassword;
	private final Button btnLoginSubmit;
	private final Button btnRegisterSubmit;
	private final Label lblRegistered;
	
	private final HeadingLabel lblWelcome; 
	private final HeadingLabel lblHeadLogin;
	private final HeadingLabel lblHeadRegister;
	
	private final EventBus ebus;
	private final RecipesView recipes;
	private final ShelvesView shelves;
	private final ShelvesPresenter shelvesPresenter;
	private final RecipesPresenter recipesPresenter;
	
	
	@UiField
	HTMLPanel content;
	
	private static PageContentViewUiBinder uiBinder = GWT
			.create(PageContentViewUiBinder.class);

	interface PageContentViewUiBinder extends UiBinder<Widget, PageContentView> {
	}

	@Inject
	public PageContentView(final EventBus ebus,
					RecipesView recipes,
					ShelvesView shelves,
					ShelvesPresenter shelvesPresenter,
					RecipesPresenter recipesPresenter) {
		
		initWidget(uiBinder.createAndBindUi(this));
		
		this.ebus = ebus;
		
		this.recipes = recipes;
		this.shelves = shelves;
		this.recipesPresenter = recipesPresenter;
		this.shelvesPresenter = shelvesPresenter;
		
		lblHeadLogin = new HeadingLabel("Login");
		lblHeadRegister = new HeadingLabel("Registration");
		
		lblName = new Label("User Name: ");
		lblPassword = new Label("Password: ");
		
		tbName = new TextBox();
		tbPassword = new PasswordTextBox();
		tbFullName = new TextBox();
		lblFullName = new Label("Full Name: ");
		btnLoginSubmit = new Button("Login");
		btnRegisterSubmit = new Button("Register");
		
		lblWelcome = new HeadingLabel("Welcome to Smart Refrigerator!");
		
		lblRegistered = new Label("Registered successfully");
		
		btnLoginSubmit.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				ebus.fireEvent(new LoginEvent());
			}
			
		});
		
		btnRegisterSubmit.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				ebus.fireEvent(new RegisterEvent());				
			}
			
		});
	}

	public void setLoginView(){
		
		content.clear();
		
		content.add(lblHeadLogin);
		
		Grid grid = new Grid(3,2);
		grid.setWidget(0, 0, lblName);
		grid.setWidget(0, 1, tbName);
		grid.setWidget(1, 0, lblPassword);
		grid.setWidget(1, 1, tbPassword);
		grid.setWidget(2, 1, btnLoginSubmit);
		
		content.add(grid);
		
	}
	
	public void setRegisterView(){
		content.clear();
		
		content.add(lblHeadRegister);
		
		Grid grid = new Grid(4,2);
		grid.setWidget(0, 0, lblName);
		grid.setWidget(0, 1, tbName);
		grid.setWidget(1, 0, lblFullName);
		grid.setWidget(1, 1, tbFullName);
		grid.setWidget(2, 0, lblPassword);
		grid.setWidget(2, 1, tbPassword);
		grid.setWidget(3, 1, btnRegisterSubmit);
		
		content.add(grid);
	}
	
	public void setRegisteredView(){
		content.clear();
		
		content.add(lblRegistered);
	}
	
	public void setRecipesView(){
		content.clear();
		
		content.add(recipes);
	}
	
	public void setShelvesView(){
		content.clear();
		
		content.add(shelves);
	}
		
	public void setWelcomeView(){
		content.clear();
		
		content.add(lblWelcome);
		
	}
	
	public String getFullName(){
		return tbFullName.getText();
	}
	
	public String getName(){
		return tbName.getText();
	}
	
	public String getPassword(){
		return tbPassword.getText();
	}

}
